import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:sl_flutter/utils/tokenManager.dart';

class SystemSettingsPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.grey[100],
      appBar: AppBar(
        leading: IconButton(
          icon: Icon(Icons.arrow_back),
          onPressed: () {
            Navigator.pop(context);
          },
        ),
        actions: [
          IconButton(
            icon: Icon(Icons.settings),
            onPressed: () {},
          ),
        ],
        title: Text('系统设置'),
        centerTitle: true,
      ),
      body: Column(
        children: [
          SingleChildScrollView(
            child: Container(
              margin: EdgeInsets.all(20),
              padding: EdgeInsets.only(
                top: 5,
                left: 5,
                right: 5,
                bottom: 5,
              ),
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(10),
                color: Colors.white,
              ),
              child: Column(
                children: [
                  _buildSettingItem('换绑手机', () {}),
                  _buildSettingItem('修改密码', () {}),
                  _buildSettingItem('消息通知设置', () {}),
                  _buildSettingItem('修改密码', () {}),
                  _buildSettingItem('清理缓存', () {}),
                ],
              ),
            ),
          ),
          Container(
            child: _buildLogoutButton(context),
          ),
        ],
      ),
    );
  }

  Widget _buildSettingItem(String title, VoidCallback onTap) {
    return Container(
        decoration: BoxDecoration(
          color: Colors.white,
        ),
        child: Container(
          decoration: BoxDecoration(
            border: Border(
              bottom: BorderSide(
                color: Colors.grey.withOpacity(0.1), // 浅黑色下划线
                width: 1, // 下划线宽度
              ),
            ),
          ),
          child: ListTile(
            title: Text(title),
            trailing: Icon(Icons.chevron_right),
            onTap: onTap,
          ),
        ));
  }

  Widget _buildLogoutButton(BuildContext context) {
    return Container(
        decoration: BoxDecoration(),
        width: double.infinity,
        padding: EdgeInsets.symmetric(horizontal: 20),
        child: Container(
          child: ElevatedButton(
              style: ElevatedButton.styleFrom(
                backgroundColor: const Color.fromARGB(255, 255, 255, 255),
                foregroundColor: Colors.black,
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(10),
                ),
              ),
              onPressed: () {
                showDialog(
                  // 设置白色
                  barrierColor:
                      const Color.fromARGB(255, 0, 0, 0).withOpacity(0.5),

                  context: context,
                  builder: (BuildContext context) {
                    return AlertDialog(
                      backgroundColor: Colors.white,
                      content: Column(
                          mainAxisAlignment: MainAxisAlignment.center,
                          mainAxisSize: MainAxisSize.min,
                          crossAxisAlignment: CrossAxisAlignment.center,
                          children: [
                            SizedBox(height: 20),
                            Text(
                              '确认要退出登录吗？',
                              style: TextStyle(fontSize: 16),
                            )
                          ]),

                      // 添加两个按钮

                      actions: [
                        Row(
                          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                          children: [
                            TextButton(
                              style: TextButton.styleFrom(
                                shape: RoundedRectangleBorder(),
                                foregroundColor: Colors.black,
                                // 设置确定按钮颜色为红色
                              ),
                              onPressed: () {
                                Navigator.of(context).pop(); // 关闭对话框
                              },
                              child: Text('取消'),
                            ),
                            TextButton(
                              style: TextButton.styleFrom(
                                foregroundColor: Colors.red, // 设置确定按钮颜色为红色
                              ),
                              onPressed: () async {
                                // 清除本地存储的 token

                                await TokenManager().removeToken();
                                // 关闭弹窗
                                Navigator.of(context).pop();
                                // 跳转到登录页面
                                Navigator.pushReplacementNamed(
                                    context, '/login');
                              },
                              child: Text('确定'),
                            ),
                          ],
                        )
                      ],
                    );
                  },
                );
              },
              child: Container(
                  height: 50,
                  // color: Colors.amber,
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [Text('退出')],
                  ))),
        ));
  }
}
